package itsm.isperp.module.controller.wechat.cp;

import itsm.isperp.framework.template.TemplateException;
import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.wechat.cp.WechatCpMenu;
import itsm.isperp.module.service.wechat.cp.WechatCpMenuService;

import javax.servlet.http.HttpServletRequest;

import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 微信菜单管理
 * 
 * 
 */
@Controller
@RequestMapping("wechat/cp/menu")
public class WechatCpMenuController extends TreeNodeController<WechatCpMenu> {

	@Autowired
	protected WechatCpMenuService wechatCpMenuService;

	@Autowired
	WxCpService wxCpService;

	@Override
	public WechatCpMenuService getService() {
		return this.wechatCpMenuService;
	}

	@Override
	public String getPageTitle() {
		return "微信企业号菜单";
	}

	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req) {
		ModelAndView mv = super.edit(id, req);
		WechatCpMenu model = (WechatCpMenu) mv.getModel().get("model");

		if ("click".equals(model.getMenuType())) {

			String replayType = model.getReplayType();
			if ("news".equals(replayType)) {

			}
		}

		return mv;
	}

	/**
	 * 将自定义菜单同步到微信接口
	 * 
	 * @return
	 * @throws TemplateException
	 * @throws WxErrorException
	 */
	@ResponseBody
	@RequestMapping("createWechatCpMenu")
	public ResultMessage createWechatCpMenu(HttpServletRequest request)
			throws TemplateException, WxErrorException {

		wxCpService.menuCreate(this.getService().createWechatMenu());

		return ResultMessage.success();

	}

	/**
	 * 获取微信菜单
	 * 
	 * @return
	 * @throws WxErrorException
	 */
	@ResponseBody
	@RequestMapping("getWechatCpMenu")
	public ResultMessage getWechatCpMenu() throws WxErrorException {
		WxMenu result = wxCpService.menuGet();
		return ResultMessage.success(result);
	}
}
